/*********************************************
Purpose : Trigger to Update Booking Details on Opportunity
Project Name: OOR Phase 2
Author: Accenture IDC
*********************************************/
trigger OORBookingUpdate on Opportunity (before insert,before update) {

List<Opportunity> Opps = new List<Opportunity>();
List<Booking_Dates__c> books  = new List<Booking_Dates__c>();
Map<String,Date> bookingdatesmap = New Map<String,Date>();
List<String> MonthList = new List<String>();
Map<String,Integer> MonthYearMap = new Map<String,Integer>();


ID SFAOORLeadOrderAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Lead Order');
ID SFAOORRenewalecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Renewals');
Set<String> Yearset = new  Set<String>();

    for(Opportunity opp:Trigger.new){
      if(opp.RecordtypeId == SFAOORLeadOrderAdvertiserRecordTypeID || opp.RecordtypeId == SFAOORRenewalecordTypeID){
        if(Trigger.ISINSERT){
            Opps.add(Opp);
            
             //Added Condition for Completed Date
          if(Opp.Order_Completion_Date__c != NULL){
               Yearset.add(String.ValueOf(opp.Order_Completion_Date__c.year()));
               Yearset.add(String.ValueOf(opp.Order_Completion_Date__c.year()+1));
              }
              
          else If(Opp.SOLO_Submitted_Date__c != NULL){
               Yearset.add(String.ValueOf(opp.SOLO_Submitted_Date__c.year()));
               Yearset.add(String.ValueOf(opp.SOLO_Submitted_Date__c.year()+1));
              }
          else If(Opp.Order_Date_SOLO__c != NULL){
               Yearset.add(String.ValueOf(opp.Order_Date_SOLO__c.year()));
               Yearset.add(String.ValueOf(opp.Order_Date_SOLO__c.year()+1));
              }
          else{
               Yearset.add(String.ValueOf(opp.CloseDate.year()));
               Yearset.add(String.ValueOf(opp.CloseDate.year()+1));
              }
        }
        if(Trigger.ISUPDATE && ((Opp.CloseDate != Trigger.OldMap.get(opp.id).CloseDate)||(Opp.Order_Date_SOLO__c != Trigger.OldMap.get(opp.id).Order_Date_SOLO__c)||(Opp.SOLO_Submitted_Date__c != Trigger.OldMap.get(opp.id).SOLO_Submitted_Date__c)||(Opp.Order_Completion_Date__c != Trigger.OldMap.get(opp.id).Order_Completion_Date__c))){
            Opps.add(Opp);
            //Added Condition for Completed Date
          if(Opp.Order_Completion_Date__c != NULL){
               Yearset.add(String.ValueOf(opp.Order_Completion_Date__c.year()));
               Yearset.add(String.ValueOf(opp.Order_Completion_Date__c.year()+1));
              }
            
          else If(Opp.SOLO_Submitted_Date__c != NULL){
               Yearset.add(String.ValueOf(opp.SOLO_Submitted_Date__c.year()));
               Yearset.add(String.ValueOf(opp.SOLO_Submitted_Date__c.year()+1));
              }
          else If(Opp.Order_Date_SOLO__c != NULL){
               Yearset.add(String.ValueOf(opp.Order_Date_SOLO__c.year()));
               Yearset.add(String.ValueOf(opp.Order_Date_SOLO__c.year()+1));
              }
          else{
               Yearset.add(String.ValueOf(opp.CloseDate.year()));
               Yearset.add(String.ValueOf(opp.CloseDate.year()+1));
              } 
        }
      }

    }
  Date Today = Date.Today();
  String year = String.ValueOf(Today.Year());
  If(Yearset.size()>0){
  books = [select Id,Booking_Closing_Year1__c,Booking_Closing_Month__c,Booking_Closing_Date__c from Booking_Dates__c where Booking_Closing_Year1__c IN:Yearset ORDER BY Booking_Closing_Date__c ASC];   
  }
      
      for(Booking_Dates__c  bookingDate:books){
          
         
                bookingdatesmap.put(bookingDate.Booking_Closing_Month__c+bookingDate.Booking_Closing_Year1__c,bookingDate.Booking_Closing_Date__c);
                //MonthMap.put(i,bookingDate.Booking_Closing_Month__c+bookingDate.Booking_Closing_Year1__c);
                MonthList.add(bookingDate.Booking_Closing_Month__c+bookingDate.Booking_Closing_Year1__c);
                MonthYearMap.put(bookingDate.Booking_Closing_Month__c+bookingDate.Booking_Closing_Year1__c,Integer.ValueOf(bookingDate.Booking_Closing_Month__c));
                
        
           
      }
      //Loop through the Opportunities and populate the Booked Date and Booking closing date
      
     
      for(Opportunity Opp:Opps){
         Date submissiondate;
         boolean NullCheck = FALSE;
         //MT:Updated this Line for completion Date Check

          if(Opp.Order_Completion_Date__c != NULL){
           submissiondate=Opp.Order_Completion_Date__c; 
           }
        
          else If(Opp.SOLO_Submitted_Date__c != NULL){
               submissiondate=Opp.SOLO_Submitted_Date__c;
          }
          else If(Opp.Order_Date_SOLO__c != NULL){
               submissiondate=Opp.Order_Date_SOLO__c;
          }
          else{
               submissiondate=Opp.CloseDate;
              }
          
          
          for(integer j=0;j<bookingdatesmap.size();j++){
                 string month = MonthList[j];
                 
                 //If(submissiondate.Month() == Integer.ValueOf(month) || submissiondate.Month()< Integer.ValueOf(month)){     
                  If(bookingdatesmap.ContainsKey(String.ValueOf(submissiondate.Month())+String.ValueOf(submissiondate.Year()))){   
                      NullCheck = TRUE;  
                  if(Opp.Order_Completion_Date__c != NULL){
                     
                      if(((Opp.Order_Completion_Date__c==bookingdatesmap.get(month))||(Opp.Order_Completion_Date__c<bookingdatesmap.get(month)))){
                                     Opp.Booking_Closing_Date__c = bookingdatesmap.get(month);
                            
                                
                                     integer noOfDays = Date.daysInMonth(submissiondate.year(), MonthYearMap.get(month));
                                     Opp.Booked_Date__c = Date.newInstance(submissiondate.year(),MonthYearMap.get(month), noOfDays);
                                break;
                           
                         
                        
                        }
                        else{
                                     Opp.Booking_Closing_Date__c = NULL;
                                     Opp.Booked_Date__c = NULL;                                          
                            }
                                              
                       

                   }
                   // This code is when completion date is null
                   else{
                       If((String.ValueOf(submissiondate.Month())+String.ValueOf(submissiondate.Year())==month)){
                         Opp.Booking_Closing_Date__c = bookingdatesmap.get(month);
                          if(bookingdatesmap.get(month) != NULL){
                                     integer noOfDays = Date.daysInMonth(submissiondate.year(), MonthYearMap.get(month));
                                     Opp.Booked_Date__c = Date.newInstance(submissiondate.year(),MonthYearMap.get(month), noOfDays);
                                break;
                           
                            
                           } 
                      }
                      else{
                          Opp.Booking_Closing_Date__c = NULL;
                          Opp.Booked_Date__c = NULL;
                      }
                   }
                
               } 
          
           }
           
          if(NullCheck == FALSE){
              Opp.Booking_Closing_Date__c = NULL;
              Opp.Booked_Date__c = NULL;
          }
      }
      
}